<template>
  <div id="Home">
    <el-container>
      <!-- 头部区域 -->
      <el-header>
        <div>
          <img src="@/assets/logo.png" alt="" />
          <span>随心购后台管理系统</span>
        </div>
        <el-button type="text" @click="logout" class="header_btn">退出登录</el-button>
      </el-header>
      <el-container>
        <!-- 侧边栏区域 -->
        <el-aside>
          <el-menu background-color="#d3dce6" unique-opened router :default-active="activePath">
            <!-- 一级菜单 -->
            <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
              <template slot="title">
                <i :class="icons[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="'/' + itemCh.path" v-for="itemCh in item.children" :key="itemCh.id"
                @click="saveStatus('/' + itemCh.path)">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ itemCh.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 主体区域 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<!-- :left="isCollapse ? '64px' : '200px'" -->
<script>
export default {
  name: 'Home',
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('path')
  },
  data() {
    return {
      menuList: [],
      icons: {
        125: 'iconfont icon-yonghuguanli_huaban',
        103: 'iconfont icon-quanxianguanli',
        101: 'iconfont icon-shangpinguanli',
        102: 'iconfont icon-dingdanguanli',
        145: 'iconfont icon-shujuguanli'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      if (window.sessionStorage.token) return this.$message.error('退出登录失败')
      this.$message.success('退出登录成功')
      this.$router.replace('/login')
    },
    async getMenuList() {
      const res = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      console.log(res.data)
      this.menuList = res.data
    },
    saveStatus(path) {
      window.sessionStorage.setItem('path', path)
      this.activePath = path
    }
  }
}
</script>

<style lang="less" scoped>
#Home {
  background-color: gray;
  height: 100vh;
}

.aside_btn {
  background-color: #add5e8;
  line-height: 25px;
  text-align: center;
  cursor: pointer;
  letter-spacing: 0.25em;
}

.aside_btn:hover {
  background-color: skyblue;
}

.aside_btn:active {
  background-color: #2775b6;
}

.header_btn {
  margin-right: 15px;
}

.el-header {
  background-color: #b3c0d1;
  line-height: 60px;
  display: flex;
  justify-content: space-between;
  color: white;
  font-size: 24px;

  >div {
    display: flex;
    // height: 50px;

    img {
      height: 100%;
    }

    span {
      padding: 0 10px;
    }
  }
}

/deep/.el-container.is-vertical {
  height: 100vh;
}

.el-aside {
  background-color: #d3dce6;
}

.el-main {
  background-color: #e9eef3;
}

.iconfont {
  margin-right: 15px;
  font-weight: 600;
  font-size: 16px;
}

/deep/.el-menu {
  border-right: 0px;
  width: 200px;
}

.el-header {
  position: relative;
  width: 100%;
  height: 60px;
}

.el-aside {
  display: block;
  position: absolute;
  left: 0;
  top: 60px;
  bottom: 0;
}

.el-main {
  position: absolute;
  left: 200px;
  right: 0;
  top: 60px;
  bottom: 0;
  overflow-y: scroll;
  min-width: 1200px;
}

/deep/.el-card {
  margin-top: 15px;
}

/deep/.cell {
  text-align: center;
}
</style>
